package com.jalarbee.core.types;

import com.jalarbee.stock.product.business.Quantity;
import com.jalarbee.stock.product.business.Unit;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;

public class QuantityType implements CompositeUserType {

    public Object assemble(Serializable arg0, SessionImplementor arg1,
            Object arg2) throws HibernateException {
        return null;
    }

    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    public Serializable disassemble(Object value, SessionImplementor session)
            throws HibernateException {
        return (Serializable) value;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y) {
            return true;
        } else if (x == null || y == null) {
            return false;
        }
        return x.equals(y);
    }

    public String[] getPropertyNames() {
        return new String[]{"count", "unit"};
    }

    public Type[] getPropertyTypes() {
        return new Type[]{Hibernate.INTEGER, Hibernate.custom(UnitType.class)};
    }

    public Object getPropertyValue(Object component, int property)
            throws HibernateException {
        Quantity quantity = (Quantity) component;
        if (property == 0) {
            return quantity.getCount();
        }
        return quantity.getUnit().getSymbol();
    }

    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

    public boolean isMutable() {
        return false;
    }

    public Object nullSafeGet(ResultSet resultSet, String[] names,
            SessionImplementor session, Object owner) throws HibernateException,
            SQLException {
        int count = resultSet.getInt(names[0]);
        Unit unit = Unit.valueOf(resultSet.getString(names[1]));
        return new Quantity(count, unit);
    }

    public void nullSafeSet(PreparedStatement statement, Object value, int index,
            SessionImplementor session) throws HibernateException, SQLException {
        if (value == null) {
            statement.setNull(index, Hibernate.INTEGER.sqlType());
            statement.setNull(index + 1, UnitType.sqlType());
        } else {
            Quantity quantity = (Quantity) value;
            statement.setInt(index, quantity.getCount());
            statement.setObject(index + 1, quantity.getUnit().getSymbol());
        }
    }

    public Object replace(Object original, Object arg1, SessionImplementor session,
            Object target) throws HibernateException {
        return original;
    }

    public Class<?> returnedClass() {
        return Quantity.class;
    }

    public void setPropertyValue(Object component, int property, Object value)
            throws HibernateException {
        throw new UnsupportedOperationException("Immutable Quantity!!");
    }
}
